package com.yiren.dbaa.module.project_edit.view.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yiren.dbaa.R;
import com.yiren.dbaa.module.project_edit.bean.ExpandableBean;

import java.util.List;

/**
 * 作者: lwb
 * 时间: 2022/11/1 9:37
 * 描述: 自定义可展开列表的适配器
 */
public class ExpandableAdapter extends BaseExpandableListAdapter {

    /**
     * 数据集合
     */
    private List<ExpandableBean> mExpandableBeanList;
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 选中的一级
     */
    private int selectGroupPosition = 0;
    /**
     * 选中的二级
     */
    private int selectChildPosition = 0;

    public ExpandableAdapter(List<ExpandableBean> expandableBeanList, Context context) {
        mExpandableBeanList = expandableBeanList;
        mContext = context;
    }

    /**
     * @return 返回组item数量
     */
    @Override
    public int getGroupCount() {
        return mExpandableBeanList == null ? 0 : mExpandableBeanList.size();
    }

    /**
     * 根据出入的组groupPosition返回子item的数量。
     *
     * @param groupPosition groupPosition
     * @return 返回子item的数量。
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        if (mExpandableBeanList == null) return 0;
        List<ExpandableBean.ChildDataBean> childDataBeans = mExpandableBeanList.get(groupPosition).getChildData();
        return childDataBeans == null ? 0 : childDataBeans.size();
    }

    /**
     * 获取组实体对象
     *
     * @param groupPosition groupPosition
     * @return 返回组实体对象
     */
    @Override
    public Object getGroup(int groupPosition) {
        return mExpandableBeanList == null ? null : mExpandableBeanList.get(groupPosition);
    }

    /**
     * 获取子实体对象
     *
     * @param groupPosition groupPosition
     * @param childPosition childPosition
     * @return 返回子实体对象
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mExpandableBeanList == null) return null;
        List<ExpandableBean.ChildDataBean> childDataBeans = mExpandableBeanList.get(groupPosition).getChildData();
        return childDataBeans == null ? null : childDataBeans.get(childPosition);
    }

    /**
     * 获取组id,我理解为获取组的唯一ID，一般自己返回groupPosition即可
     *
     * @param groupPosition groupPosition
     * @return Position
     */
    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    /**
     * 获取子ID
     *
     * @param groupPosition groupPosition
     * @param childPosition childPosition
     * @return 返回子id
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }


    /**
     * 用来判断ExpandableListView内容id是否有效的,我也不太明白，它具体有什么作用，设置返回true和false的效果是一样的。
     *
     * @return true or false
     */
    @Override
    public boolean hasStableIds() {
        return false;
    }

    /**
     * 获取给定分组的视图，主要重写的方法。
     *
     * @param groupPosition groupPosition
     * @param isExpanded    该组是展开状态还是收起状态
     * @param convertView   convertView
     * @param parent        parent
     * @return View
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_group, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvGroupName = (TextView) convertView.findViewById(R.id.item_group_name_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 获取组的实体对象
        ExpandableBean expandableBean = mExpandableBeanList.get(groupPosition);
        // 获取组名
        String groupName = expandableBean.getGroupName();
        // 设置TextView的文字
        viewHolder.tvGroupName.setText(groupName);
        if (groupPosition == selectGroupPosition) {
            viewHolder.tvGroupName.setTextColor(mContext.getResources().getColor(R.color.blue_select_text));
        } else {
            viewHolder.tvGroupName.setTextColor(mContext.getResources().getColor(R.color.title_gray));
        }
        // 设置组item展开或者关闭的右图片
        Drawable drawableRight = ContextCompat.getDrawable(mContext, groupPosition == selectGroupPosition ? R.mipmap.arrow_down_s : R.mipmap.arrow_down);
        if (isExpanded) {
            // 展开
            drawableRight = ContextCompat.getDrawable(mContext, groupPosition == selectGroupPosition ? R.mipmap.arrow_up_s : R.mipmap.arrow_up);
        }
        viewHolder.tvGroupName.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableRight, null);

        return convertView;
    }

    /**
     * 获取给定分组给定子位置的数据用的视图，主要重写的方法。
     *
     * @param groupPosition groupPosition
     * @param childPosition childPosition
     * @param isLastChild   是否为改组最后一个子视图
     * @param convertView   convertView
     * @param parent        parent
     * @return View
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_child, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvChildName = (TextView) convertView.findViewById(R.id.item_child_name_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ExpandableBean expandableBean = mExpandableBeanList.get(groupPosition);
        List<ExpandableBean.ChildDataBean> childDataBeans = expandableBean.getChildData();
        ExpandableBean.ChildDataBean childDataBean = childDataBeans.get(childPosition);
        // 获取子名字
        String childName = childDataBean.getChildName();
        // 设置文字
        viewHolder.tvChildName.setText(childName);
        if (childPosition == selectChildPosition && groupPosition == selectGroupPosition) {
            viewHolder.tvChildName.setTextColor(mContext.getResources().getColor(R.color.blue_select_text));
            viewHolder.tvChildName.setBackgroundColor(mContext.getResources().getColor(R.color.blue_select_bg));
        } else {
            viewHolder.tvChildName.setTextColor(mContext.getResources().getColor(R.color.gray_text));
            viewHolder.tvChildName.setBackground(null);
        }
        return convertView;
    }

    /**
     * 子item是否可点击
     * 当isChildSelectable方法返回false的时候，子item不可点击，且不会绘制分割线
     *
     * @param groupPosition groupPosition
     * @param childPosition childPosition
     * @return true可点击 false不可点击
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class ViewHolder {
        TextView tvGroupName;
        TextView tvChildName;
    }

    /**
     * 通过文件名，获取对应的图片资源
     *
     * @param context  context
     * @param filename filename
     * @return 对应的图片资源
     */
    public int getImageMipmapRes(Context context, String filename) {
        return context.getResources().getIdentifier(filename, "mipmap", context.getPackageName());
    }

    public void setExpandableModeList(List<ExpandableBean> expandableBeanList) {
        mExpandableBeanList = expandableBeanList;
    }

    /**
     * 刷新适配器
     */
    public void refreshAdapter(int selectGroupPosition, int selectChildPosition) {
        this.selectChildPosition = selectChildPosition;
        this.selectGroupPosition = selectGroupPosition;
        notifyDataSetChanged();
    }

}
